package com.yunzo.coc.plugin;

import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
//import com.zaxxer.hikari.HikariConfig;
//import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * *
 * @author xiaobo
 */
public class HikariCPPlugin implements IPlugin,IDataSourceProvider {

    private String jdbcUrl;
    private String user;
    private String password;
    private String driverClass = "com.mysql.jdbc.Driver";
    private int maxPoolSize = 100;
    private int minPoolSize = 5;
    private long idleTimeoutMs=60000000;
    //private HikariDataSource dataSource;
    
    public HikariCPPlugin(String jdbcUrl, String user, String password) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
    }

    public HikariCPPlugin(String jdbcUrl, String user, String password, String driverClass, int maxPoolSize) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
        this.driverClass = driverClass;
        this.maxPoolSize = maxPoolSize;
    }
    public HikariCPPlugin(String jdbcUrl, String user, String password, String driverClass, int maxPoolSize,int minPoolSize) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
        this.driverClass = driverClass;
        this.maxPoolSize = maxPoolSize;
        this.minPoolSize = minPoolSize;
    }
    
    public boolean start() {
       // HikariConfig config = new HikariConfig();
        //池中最大链接数量 
//        config.setMaximumPoolSize(maxPoolSize);
//        //池中最小空闲链接数量  
//        config.setMinimumIdle(minPoolSize); 
//       // config.setIdleTimeout(idleTimeoutMs);
////      config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
//        config.setDriverClassName(driverClass);
//        config.setJdbcUrl(jdbcUrl);
//        config.setUsername(user);
//        config.setPassword(password);
//       
//        //防止中文乱码
//        config.addDataSourceProperty("useUnicode", "true");
//        config.addDataSourceProperty("characterEncoding", "utf8");
//        
//        config.setConnectionTestQuery("SELECT 1");

      //  this.dataSource = new HikariDataSource(config);
        
        return true;
    }

    public boolean stop() {
//        if (dataSource != null)
//            dataSource.close();
        return true;
    }

	@Override
	public DataSource getDataSource() {
		// TODO Auto-generated method stub
		return null;
	}

//    public DataSource getDataSource() {
//        return this.dataSource;
//    }
}
